/**
 * 初始化订单信息详情对话框
 */

var UserInfoDlg = {
    count: $("#itemSize").val(),
    dictName: '',			//字典的名称
    mutiString: '',		//拼接字符串内容(拼接字典条目)
    editor: null,
    itemTemplate: $("#itemTemplate").html(),
    validateFields: {
        userName: {
            validators: {
                notEmpty: {
                    message: '姓名不能为空'
                }
            }
        }
    }
};


/**
 * 清除数据
 */
UserInfoDlg.clearData = function() {
    this.orderInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
UserInfoDlg.set = function(key, val) {
    this.orderInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
UserInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
UserInfoDlg.close = function() {
    parent.layer.close(window.parent.UserInfo.layerIndex);
}


/**
 * 添加条目
 */
UserInfoDlg.addItem = function () {
    $("#itemsArea").append(this.itemTemplate);
    $("#orderItem").attr("id", this.newId());
};

/**
 * 删除item
 */
UserInfoDlg.deleteItem = function (event) {
    var obj = Feng.eventParseObject(event);
    var itemPid=$(obj.parent().parent()).children("input.item-pid").val();
    if(itemPid!=null && itemPid.length>0){//不为空
        var ajax = new $ax(Feng.ctxPath + "/orderInfo/deleteItem", function(data){

        },function(data){
            Feng.error("系统异常!" + data.responseJSON.message + "!");
        });
        var datas={};
        datas.itemId = itemPid;
        ajax.set(datas);
        ajax.start();
    }
    console.log("deleteItem");
    obj.parent().parent().remove();
};

UserInfoDlg.htmlDecode=function(text) {
    var entities = {
        '&amp;': '&',
        '&gt;': '>',
        '&lt;': '<',
        '&quot;': '"'
    }, keys = [], p, regex;

    for (p in entities) {
        keys.push(p);
    }
    regex = new RegExp('(' + keys.join('|') + '|&#[0-9]{1,5};' + ')', 'g');
    return (!text) ? text : String(text).replace(regex, function(match, capture) {
        if (capture in entities) {
            return entities[capture];
        } else {
            return String.fromCharCode(parseInt(capture.substr(2), 10));
        }
    });
}

/**
 * item获取新的id
 */
UserInfoDlg.newId = function () {
    if(this.count == undefined){
        this.count = 0;
    }
    this.count = this.count + 1;
    return "orderItem" + this.count;
};

/**
 * 清除为空的item Dom
 */
UserInfoDlg.clearNullDom = function(){
    $("[name='dictItem']").each(function(){
        var num = $(this).find("[name='itemNum']").val();
        var name = $(this).find("[name='itemName']").val();
        if(num == '' || name == ''){
            $(this).remove();
        }
    });
};


/**
 * 收集添加订单信息的数据
 */
UserInfoDlg.collectData = function () {
    //debugger;
    this.clearNullDom();
    var mutiString = "";
    var orderInfosValues = "";
    $("[name='orderItem']").each(function(){
        var itemPid=$(this).find("[name='itemPid']").val();
        if(itemPid==undefined){
            itemPid=0;
        }
        var productTypeName = $(this).find("[name='itemProductType']").find("option:selected").text();
        var productType = $(this).find("[name='itemProductType']").val();
        var productModelId= $(this).find("[name='itemProductModel']").val();
        var productModel=$(this).find("[name='itemProductModel']").find("option:selected").text();
        var quantity = $(this).find("[name='itemQuantity']").val();
        var price = $(this).find("[name='itemPrice']").val();
        mutiString = mutiString + (productTypeName + "-" + productModel + "-"+ quantity +",");
        orderInfosValues=orderInfosValues+ (productType+"-"+productTypeName + "-" + productModelId + "-"+ quantity + "-"+itemPid+",");
    });
    $("#orderInfos").val(mutiString);
    $("#orderInfosValues").val(orderInfosValues);

    console.log("remarks"+UserInfoDlg.editor.txt.text());
    this.orderInfoData['remarks'] = UserInfoDlg.editor.txt.text();
    this.set('id').set('userName').set('userPhone').set('address').set('amount').set('depositAmount').set('isYumai').set('yumaiTime').set('status').set('createTime')
        .set('remainBalance').set('orderInfos').set('orderInfosValues').set('orderNo').set('salesName').set('setupName').set('salesTime').set('setupTime');
};

/**
 * 产品类型选择方法
 * listProductByType
 * @param event
 */
UserInfoDlg.itemProductTypeChange=function (event) {
    var obj = Feng.eventParseObject(event);
    var value=obj.val();
    var parent=$(obj.parent().parent()).children("div.itemProductModel").children("select");
    parent.empty();//清除之前产品
    var ajax = new $ax(Feng.ctxPath + "/product/listProductByType", function(data){
        var len=data.length;
        for(var i=0;i<len;i++){
            var option = new Option(data[i].productModel,data[i].id);//文本值
            $(parent).append(option);
        }
    },function(data){
        Feng.error("系统异常!" + data.responseJSON.message + "!");
    });
    var datas={};
    datas.productType = value;
    ajax.set(datas);
    ajax.start();
}


/**
 * 提交添加
 */
UserInfoDlg.addSubmit = function(status) {
    this.clearData();
    this.collectData();
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/userInfo/add", function(data){
        Feng.success("添加成功!");
        window.parent.UserInfo.table.refresh();
        UserInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.orderInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
UserInfoDlg.editSubmit = function(status) {
    this.clearData();
    $("#status").val(status);
    this.collectData();
    //提交信息
    if(status==1){//下单
        var ajax = new $ax(Feng.ctxPath + "/userInfo/update", function(data){
            Feng.success("修改成功!");
            window.parent.UserInfo.table.refresh();
            UserInfoDlg.close();
        },function(data){
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.set(this.orderInfoData);
        ajax.start();
    }else if(status==0){//修改客户信息
        var ajax = new $ax(Feng.ctxPath + "/userInfo/updateUserInfo", function(data){
            Feng.success("修改成功!");
            window.parent.UserInfo.table.refresh();
            UserInfoDlg.close();
        },function(data){
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        var content=this.orderInfoData.remarks;
        console.log("content:"+content);
        this.orderInfoData['content']=content;
        ajax.set(this.orderInfoData);
        ajax.start();
    }
}




$(function() {
    Feng.initValidator("userInfoForm", UserInfoDlg.validateFields);
    //初始化编辑器
    var E = window.wangEditor;
    var editor = new E('#editor');
    editor.create();
    editor.txt.html($("#remarksVal").val());
    UserInfoDlg.editor = editor;

    //初始化产品更换周期
    if($("#isYumai").val() == undefined){
        $("#isYumai").val(1);
    }else{
        $("#isYumai").val($("#isYumaiValue").val());
    }

    //初始化状态
    if($("#status").val() == undefined){
        $("#status").val(2);
    }else{
        $("#status").val($("#statusValues").val());
    }
});